/**
 * HiLo
 * <p/>
 * User: joakimd
 * Date: 2013-02-04
 * Time: 09:55
 */
package org.dru.test {
import flash.events.SampleDataEvent;
import flash.media.Sound;

public class HiLo {
    [Embed(source="hi-lo/high_part1.mp3")]
    private var high_part1_class: Class;

    [Embed(source="hi-lo/high_part2.mp3")]
    private var high_part2_class: Class;

    [Embed(source="hi-lo/low_part1.mp3")]
    private var low_part1_class: Class;

    [Embed(source="hi-lo/low_part2.mp3")]
    private var low_part2_class: Class;

    private var _samples: Vector.<Sample>;
    private var _left: Vector.<Number>;
    private var _right: Vector.<Number>;
    private var _out: Sound;
    private var _pos: int;
    private var _patterns: Array = [
        -1, 2,
        -1, 3,
        0, -1,
        1, -1,
        0, 2,
        1, 3,
        0, 3,
        1, 2
    ];
    private var _patPos: int = 0;
    private var _numtracks: int = 2;
    private var _length: int;

    public function HiLo() {
        _samples = new Vector.<Sample>();
        _samples.push(new Sample(new low_part1_class() as Sound));
        _samples.push(new Sample(new low_part2_class() as Sound));
        _samples.push(new Sample(new high_part1_class() as Sound));
        _samples.push(new Sample(new high_part2_class() as Sound));
        _length = _samples[0].length;
        Masic.log("" + _length);
        _length = Math.min(_samples[1].length, _length);
        Masic.log("" + _length);
        _length = Math.min(_samples[2].length, _length);
        Masic.log("" + _length);
        _length = Math.min(_samples[3].length, _length);
        Masic.log("" + _length);
        _left = new Vector.<Number>(2048, true);
        _right = new Vector.<Number>(2048, true);
        _pos = 0;
        _out = new Sound();
        _out.addEventListener(SampleDataEvent.SAMPLE_DATA, onSampleData);
        _out.play();

    }

    private function getPattern(track: int): int {
        return _patterns[_patPos * _numtracks + track];
    }

    private function onSampleData(event: SampleDataEvent): void {
        var length: int = 2048;
        var offset: int = 0;
        do {
            var amount: int = Math.min(_length - _pos, length);
            var end: int = offset + amount;
            for (var sample: int = offset; sample < end; sample++) {
                _left[sample] = _right[sample] = 0.0;
            }
            for (var track: int = 0; track < _numtracks; track++) {
                var pattern: int = getPattern(track);
                if (pattern != -1) {
                    _samples[pattern].addTo(_pos, _left, _right, offset, amount);
                }
            }
            offset += amount;
            _pos += amount;
            if (_pos == _length) {
                _patPos += 1;
                if (_patPos * _numtracks == _patterns.length) {
                    _patPos = 0;
                }
                _pos = 0;
            }
            length -= amount;
        }
        while (length > 0);
        for (var index: int = 0; index < 2048; index++) {
            event.data.writeFloat(_left[index]);
            event.data.writeFloat(_right[index]);
        }
    }

    private function selectPatterns(): void {
    }
}
}
